﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using NLog;


namespace StickTops
{
    public partial class MainForm : Form
    {

        #region "Private members"

        private ArrayList StickTopList = new ArrayList();
        private Varables Vars;
        private LoginForm loginForm;
        private SaveFile File;
        private SaveSql Sql;
        private bool inLogged = false;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region "constructor"

        public MainForm()
        {
            InitializeComponent();
        }

        #endregion

        #region "Private Methods"

        private void MainForm_Load(object sender, EventArgs e)
        {
            logger.Debug("MainForm On Load Starts with autologin: " + Properties.Settings.Default.AutoLogin);
            Vars = new Varables(this, ref StickTopList);

            if (Properties.Settings.Default.AutoLogin && Properties.Settings.Default.Username != ""){

                logger.Debug("Trying to AutoLogon...");

                if (Sql == null){ Sql = new SaveSql(ref Vars);};
                Sql.LogIn(Properties.Settings.Default.Username, Properties.Settings.Default.Password);

                if (Sql.State != SaveSql.LogInState.Success)
                {
                    loginForm = new LoginForm(ref Vars);
                    loginForm.Show();
                }
                else { logger.Debug("AutoLogon failed because: " + Sql.Message); }
            }
            else {
                logger.Debug("Starting without autologin");
                loginForm = new LoginForm(ref Vars);
                loginForm.Show();
            }
        }

        private void SetMenuInfo(bool logIn)
        {
            if (inLogged)
            {
                //Log out
                if (logIn == false)
                {
                    MenuItemInfo.Text = "Log In";
                    MenuItemInfo.Enabled = true;
                    MenuItemLoadFromFile.Visible = true;
                }
            }
            else
            {
                //Log in
                if (logIn)
                {
                    MenuItemInfo.Text = Vars.UserName;
                    MenuItemInfo.Enabled = false;
                    MenuItemLoadFromFile.Visible = false;
                }
            }

        }

        private void AutoSave_Tick(object sender, EventArgs e)
        {
            if (Vars.LoggedIn)
            {
                MenuItemSave_Click(this, e);
            }
        }

        private void MenuItemSettings_Click(object sender, EventArgs e)
        {
            SettingsForm setForm = new SettingsForm();
            DialogResult savingDone = setForm.ShowDialog();
            AutoSave.Interval = Properties.Settings.Default.AutoSaveIntervall;
        }

        #endregion

        #region "Public methods"

        public void MakeNewNote(NewNote newnote)
        {
            StickTop NewStick = new StickTop(newnote,ref Vars);
            NewStick.Show();
            StickTopList.Add(NewStick);
            SetMenuInfo(true);
        }

        public void MakeNewNote()
        {
            StickTop NewStick = new StickTop(new NewNote("",Properties.Settings.Default.StartSize,
                                                            Properties.Settings.Default.StartPos,
                                                            Properties.Settings.Default.MainFore,
                                                            Properties.Settings.Default.MainBack,
                                                            Properties.Settings.Default.LabelFore,
                                                            Properties.Settings.Default.LabelBack
                                                            ),ref Vars);
            NewStick.Show();
            StickTopList.Add(NewStick);
        }

        #endregion

        #region "Menu"

        private void MenuItemInfo_Click(object sender, EventArgs e)
        {
            if (loginForm.IsClosed == true)
            {
                loginForm = new LoginForm(ref Vars);
                loginForm.Show();
            }
            else { loginForm.Show(); loginForm.BringToFront(); }
            
        }

        private void MenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void NewIconMenuItem_Click(object sender, EventArgs e)
        {
            MakeNewNote();
        }

        private void MenuItemAbout_Click(object sender, EventArgs e)
        {
            AboutBox aboutMe = new AboutBox();
            aboutMe.Show();
        }

        private void MenuItemShowAll_Click(object sender, EventArgs e)
        {
            foreach (StickTop i in StickTopList)
            {
                i.BringToFront();
            }
        }

        private void MenuItemNew_Click(object sender, EventArgs e)
        {
            MakeNewNote();
        }

        private void MenuItemSave_Click(object sender, EventArgs e)
        {
            //reset AutoSave timer
            AutoSave.Enabled = false; AutoSave.Enabled = true;

            if (File == null){ File = new SaveFile(ref Vars);}
            if (Sql == null) { Sql = new SaveSql(ref Vars); }

            //Save to file
            bool FileSaveStatus = File.SaveToFile();
            bool SqlSaveStatus;

            //Save to Sql and display the result
            if (Sql.Save(Vars.UserName) == true)
            {
                NotifyIcon.ShowBalloonTip(2000, "Save complete", "The notes are saved succesfully!", ToolTipIcon.Info);
                Vars.LoggedIn = true;
                SqlSaveStatus = true;
            }
            else
            {
                NotifyIcon.ShowBalloonTip(2000, "Save failed", "Could not save the notes because: " + Sql.Message, ToolTipIcon.Error);
                SqlSaveStatus = false;
            }
            logger.Trace("MainForm Save Result: " +FileSaveStatus.ToString() + "  SqlStatus: " + SqlSaveStatus.ToString());
        }

        private void MenuItemSetting_Click(object sender, EventArgs e)
        {
            SettingsForm aboutMe = new SettingsForm();
            aboutMe.Show();
        }

        private void MenuItemLoadFromFile_Click(object sender, EventArgs e)
        {
            if (loginForm != null){loginForm.Close();}

                if (File == null){ File = new SaveFile(ref Vars);}
                    File.LoadFromFile();
        }
        
        #endregion

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            logger.Trace("Exiting program with auto logon: " + Properties.Settings.Default.AutoLogin.ToString() + " and username: " + Properties.Settings.Default.Username);
            if (Vars.LoggedIn)
            {
                MenuItemSave_Click(this, e);
            }
            Properties.Settings.Default.Save();
        }

    }
}
